package com.weng.coder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.coder.entity.CodeTable;
import com.weng.coder.service.CodeTableService;
import com.weng.framework.core.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 表 Web API
 *
 * @作者 wengzhonghui
 * @创建时间 2018-02-24 17:16:11
 */
@RestController
@RequestMapping("/codeTable") 
@Api(value = "表相关接口")
public class CodeTableController{
	@Resource
	private CodeTableService codeTableService;
	
	 /**
	 * 获取分页数据
	 */
	 @ResponseBody
	 @RequestMapping(value="/list",method=RequestMethod.GET)
	 @ApiOperation(value = "分页获取表列表", notes = "分页获取表列表")
	 @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query",name = "pageNo",dataType ="Integer" ,required = true,value = "页数"),
        @ApiImplicitParam(paramType = "query",name = "pageSize",dataType ="Integer" ,required = true,value = "每页数量")
	 })
	 public ResponseData getDataByPage(String projectId, String remarks, String code, Integer pageNo, Integer pageSize){
		 projectId = StringUtils.isEmpty(projectId) ? "-1" : projectId;
		 
		 CodeTable codeTable = new CodeTable();
		 codeTable.setProjectId(projectId);
		 codeTable.setCode(code);
		 codeTable.setRemarks(remarks);
		 
		 IPage<CodeTable> page = codeTableService.getDataByPage(pageNo, pageSize, codeTable);
		 return ResponseData.success(page);
	 }
	
	 /**
	 * 获取表
	 * 
	 */
	@RequestMapping(value="get-one",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取表", notes = "获取表")
	public ResponseData getCodeTable(String id, Model model
			,HttpServletRequest request) throws Exception{
		if(StringUtils.isEmpty(id)){
			return ResponseData.failed("表Id不能为空！");
		}
		
		CodeTable codeTable = this.codeTableService.getOne(id);
		return ResponseData.success(codeTable);
	}
	 
	/**
	 * 添加表
	 * 
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加表", notes = "添加表")
	public ResponseData addCodeTable(CodeTable codeTable, Model model
			,HttpServletRequest request) throws Exception{
		if(codeTable==null ){
			return ResponseData.failed("表不能为空！");
		}
		codeTableService.save(codeTable);
		return ResponseData.success("添加表成功！");
	}
	
	/**
	 * 更新表
	 */
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新表", notes = "更新表")
	public ResponseData updateCodeTable(CodeTable codeTable,HttpServletRequest request) throws Exception{
		if(codeTable==null || StringUtils.isEmpty(codeTable.getId())){
			return ResponseData.failed("表不能为空！");
		}
		CodeTable oldCodeTable = this.codeTableService.getOne(codeTable.getId());
		if(oldCodeTable==null){
			return ResponseData.failed("表信息不存在，不能更新！");
		}
		codeTable.setProjectId(oldCodeTable.getProjectId());
		codeTable.setStatus(oldCodeTable.getStatus());
		codeTableService.save(codeTable);
		return ResponseData.success("更新成功！");
	}
	
	
	/**
	 * 删除表
	 */
	@RequestMapping(value="delete",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除表", notes = "删除表,多个ID以,号分隔")
	@Transactional
	public  ResponseData deletecodeTable( String ids
			,HttpServletRequest request) throws Exception{
		String[] codeTableIdList = ids.split(",");
		if(codeTableIdList == null || codeTableIdList.length < 1){
			return ResponseData.failed("表ID不能为空");
		}
		StringBuilder logDesc = new StringBuilder();
		List<CodeTable> codeTableList = codeTableService.findByIds(Arrays.asList(codeTableIdList));
		if(codeTableList!=null && codeTableList.size()>0){
			for(CodeTable codeTable:codeTableList){
				if(logDesc.length()>0){
					logDesc.append(",");
				}
				logDesc.append(codeTable.getId());
				this.codeTableService.delete(codeTable);
			}
		}
		return ResponseData.success("删除成功");
	}


	/**
	 * 通过SQL添加表和列属性
	 * 
	 */
	@RequestMapping(value="add-table-by-sql",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "通过建库SQL添加表", notes = "通过建库SQL添加表")
	public ResponseData addCodeTableBySql(String projectId,String area,String sql, Model model
			,String entityName, String methodName, String methodType, String remark,HttpServletRequest request){
		ResponseData result = this.codeTableService.addCodeTableAndFieldsBySql(projectId, area, sql, model,entityName, methodName, methodType, remark,request);
		return result;
	}
	
}

